package de.wolvan.autoupdate;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URLDecoder;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import de.wolvan.references.*;
import de.wolvan.tools.*;
import de.wolvan.window.InfoWindow;
import de.wolvan.window.*;

public class UpdateGUI extends JFrame implements ActionListener{
	
	public static UpdateGUI frame;
	
	public static JButton updateYes;
	public static JButton updateNo;
	
	public static JEditorPane infoText;
	public static JScrollPane scrollInfoText;
	
	public static JLabel label;
	
	public static JTextArea ChangelogFetchFailed;
	
	public static void renderGUI() {
		frame = new UpdateGUI("New Update available");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(800,610);
		frame.setLayout(null);
		frame.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == updateNo) {
			frame.setVisible(false);
			LoginWindow.renderGUI();
		}
		if(e.getSource() == updateYes) {
			try {
				String updateFileURL = UpdateChecker.getUpdatePackageURL();
				UpdateFile.downloadFile(updateFileURL.split("/")[updateFileURL.split("/").length - 1], updateFileURL);
				String path = de.wolvan.main.main.class.getProtectionDomain().getCodeSource().getLocation().getPath();
				String decodedPath = URLDecoder.decode(path, "UTF-8");
				String[] cmd = { "java", "-jar", updateFileURL.split("/")[updateFileURL.split("/").length - 1], "removeOldVersion;" + decodedPath};
				Runtime.getRuntime().exec(cmd);
				System.exit(0);
			} catch (MalformedURLException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	public UpdateGUI(String title) {
		super(title);
		
		updateNo = new JButton("Do not Update");
		updateNo.setBounds(10,520,370,40);
		updateNo.addActionListener(this);
		add(updateNo);
		
		updateYes = new JButton("Update");
		updateYes.setBounds(400,520,370,40);
		updateYes.addActionListener(this);
		add(updateYes);
		
		ChangelogFetchFailed = new JTextArea("");
		ChangelogFetchFailed.setBounds(10,10,760,500);
		ChangelogFetchFailed.setVisible(false);
		add(ChangelogFetchFailed);
		
		try {
			infoText = new JEditorPane(ProgramVars.htmlChangelogURL);
			infoText.setBounds(ChangelogFetchFailed.getBounds());
			infoText.setEditable(false);
			add(infoText);
			
			scrollInfoText = new JScrollPane(infoText);
			scrollInfoText.setBounds(infoText.getBounds());
			add(scrollInfoText);
		} catch (IOException e) {
			//infoText.setVisible(false);
			String Error = "";
			for(int i = 0; i < e.getStackTrace().length;i++) {
				Error += e.getStackTrace()[i] + "\n";
			}
			System.out.println("Error while fetching Changelog");
			e.printStackTrace();
			ChangelogFetchFailed.setText("Failed to fetch Changelog.\nMaybe there is a problem with the Internet connection or the Site is down\n\n\nError Stack Trace:\n" + Error);
			ChangelogFetchFailed.setVisible(true);
			scrollInfoText = new JScrollPane(ChangelogFetchFailed);
			scrollInfoText.setBounds(ChangelogFetchFailed.getBounds());
			add(scrollInfoText);
		}
		
		
		label = new JLabel("A new update has been released. Do you want to download it?");
		label.setBounds(0,0,0,0);
		add(label);
	}
}
